<?php

/**
 * 商户验卡,包含同步商户资料以及验卡,第一次验卡是新增商户以及验卡,成功后再次调用验卡接口则是修改商户及验卡
 */
include_once '../Util/Common.php';
require_once __DIR__ . './../Util/Utils.php';

use UtilMethod\Utils;

$utils = new Utils();

$post_data = [
    'account' => ACCOUNT,
    'orderCode' => 'tb_verifyInfo'
];

$msgDate = [
    'real_name' => base64_encode("王胜"), //真实姓名
    'cmer' => base64_encode("上海WS电子科技有限公司"), //商户全称
    'cmer_sort' => base64_encode("WS电子科技"), //商户简称
    'channel_code' => 'WXPAY', //通道标识
    'business_id' => 53, //经营类目(传对应的微信MCC)
    'phone' => '15800468784', //商户联系电话
    'card_type' => '1', //结算卡类型(默认值1,借记卡)
    'card_no' => '6217857600015915353', //结算卡号
    'cert_type' => '00', //身份证件号类型(默认值00,身份证号)
    'cert_no' => '421081199309212975', //身份证件号码
    'mobile' => '15800468784', //结算卡开户手机号
    'location' => base64_encode("武汉"), //结算卡开户城市
    //非必填,默认为空
    'cert_correct' => '',
    'cert_opposite' => '',
    'cert_meet' => '',
    'card_correct' => '',
    'card_opposite' => ''
];

$priKeyFilePath = PRIVATEKEYPATH . $post_data['account'] . "_private_key.pem";
$sign = $utils::rsaDataSign(json_encode($msgDate, JSON_UNESCAPED_SLASHES), $priKeyFilePath); //RSA签名

$post_data['msg'] = json_encode($msgDate, JSON_UNESCAPED_SLASHES);

$data = base64_encode(json_encode($post_data, JSON_UNESCAPED_SLASHES));
$decrypt = $utils::rsaPublicEncrypt($data, PUBLICKEYPATH); //RSA公钥加密

$send_data = [
    'data' => $decrypt,
    'signature' => $sign
];

$res = $utils::send_post1(KUBEI, json_encode($send_data, JSON_UNESCAPED_SLASHES));
$res = json_decode($res, true);
$res_data = $res['data'];
$res_sign = $res['signature'];
$original = $utils::rsaPrivateDecrypt(base64_decode($res_data), $priKeyFilePath); //RSA私钥解密
$original = json_decode($original, true);
$res_msg = json_decode($original['msg'], true);

//验证签名
$valid = $utils::isValid(json_encode($res_msg, JSON_UNESCAPED_SLASHES), base64_decode($res_sign), PUBLICKEYPATH);


if ("000000" === $res_msg['respCode']) {
    print("验卡成功.");
} else {
    print("验卡失败.返回码:" . $res_msg['respCode'] . ",失败原因:" . $res_msg['respInfo'] . "<br/>");
    var_dump($res_msg);
}